

import gameShareAdsVideo from "./gameShareAdsVideo";
import PlayData from "./PlayData";


export default class gameCheckLogin  {
  
    static serverUrl:"https://www.1111.top/rank"
    static gameCode: any;
    static anonymousCode: any;

    static getPlatformClass(){
        return gameShareAdsVideo.Instance.getPlatformClass()
    }

  /**
   * 游戏登录
   */
  static gameLogin(_force){
    if(gameShareAdsVideo.Instance.isdebug) return
      //console.log('游戏登录')
      var self = this
      if(cc.sys.platform == cc.sys.WECHAT_GAME ) {
        //@ts-ignore
        wx.login({
          success (res) {
            if (res.code) {
              //发起网络请求
              self.gameCode = res.code
              console.log('login 调用成功',res);
            } else {
              console.log('登录失败！' + res.errMsg)
            }
          }
        })
      }
      else if(cc.sys.platform == cc.sys.BYTEDANCE_GAME ){
        
      }else{
        return false
      }
  }

  /**
   * 获取用户信息 
   */
  static wx_getUserInfo(callback){
    if(cc.sys.platform == cc.sys.WECHAT_GAME){
        //获取设置
        //console.log('获取用户信息')
        //@ts-ignore
        wx.getSetting({
          success(res){
              //console.log('获取用户权限设置成功2', res.authSetting['scope.userInfo'])
              if(res.authSetting['scope.userInfo'] == false){ //询问是否打开设置界面
                //@ts-ignore
                wx.showModal({
                      title: "提示！",
                      content: "用户权限未打开，请设置？",
                      success(res) {
                        if (res.confirm) {
                          //console.log("confirm, continued");  //确定设置，打开设置界面
                          //@ts-ignore
                          wx.openSetting({
                              success(res){
                                  //console.log('设置结果',res)
                              },
                              fail(){

                              }
                          })
                        } else if (res.cancel) {
                          //console.log("cancel, cold");
                        } else {
                          // what happend?
                        }
                      },
                      fail(res) {
                        //console.log(`showModal调用失败`);
                      },
                    });
              }
              else { //直接请求数据 
                //console.log('用户信息', gameConfig.userInfo)
                if(PlayData.Instance.userInfo.name == ''){
                  //console.log('直接请求用户信息')
                  //@ts-ignore
                  wx.getUserProfile({
                    desc:'排行榜需要获取用户信息',
                    success(res) {
                      console.log('获取用户信息成功', res);
                      //window.userinfo = res.userInfo
                      
                      PlayData.Instance.userInfo.icon = res.userInfo.avatarUrl
                      PlayData.Instance.userInfo.name = res.userInfo.nickName
                      
                      //console.log('获取用户信息成功', res.userInfo)
                      callback()

                      //Storage.setUserInfo()
                      let info = JSON.stringify(PlayData.Instance.userInfo)
                      cc.sys.localStorage.setItem('userinfo', info)
                    },
                    fail(res) {
                      //console.log('getUserInfo 调用失败',res);
                    },
                  });
                }else{callback()}
              }
          },fail(){
              //console.log('获取用户权限设置失败')
          }
      })
    }else{
      callback()
    }
    
  }
}
